package br.usp.pcs.basys.engine;

import java.util.PriorityQueue;
import java.util.Queue;

public class ComputerResourceManager<Resource extends ComputerResource,Request> {

	protected boolean busy;
	protected ComputerResource resource;
	private Queue<Request> resourceQueue = new PriorityQueue<Request>();

	public ComputerResourceManager() {
		super();
	}

	public boolean isBusy() {
		return busy;
	}

	private void setBusy(boolean busy) {
		this.busy = busy;
	}

	public void reserve() throws UnavaiableResourceException {
		if (!busy){
			setBusy(true);
		}
		else {
			throw new UnavaiableResourceException();
		}
	}

	public ComputerResource getResource() {
		return resource;
	}

	public void release() {
		setBusy(false);
	}

	public boolean queue(Request r) {
		return resourceQueue.add(r);
	}

	public Request poll() {
		return resourceQueue.poll();
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

}